import type { IColumnsOption } from 'vc';
import { getImageUrl, handleTagNegativeShow } from '@/utils';
import { INDEX, COMPANY_NAME, INFO_SOURCE, PROVINCE, CITY, COUNTRY, INDUSTRY_CATEGORY, BUSINESS_DATA, REGISTERED_CAPITAL } from '@/utils';

export function useTable() {
  const tableData = ref([]);

  const columns = ref<IColumnsOption[]>([
    { ...INDEX },
    { ...COMPANY_NAME },
    {
      title: '评价年度',
      field: 'tagYear',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'tagYear'
    },
    {
      title: '评价等级',
      field: 'tagNegative',
      width: 100,
      align: 'center',
      slots: {
        default: ({ row }) => {
          const map = {
            良好: 'table-level-1e83eb',
            诚信: 'table-level-28b97f',
            一般: 'table-level-ff9d54',
            警示: 'table-level-ff6b6a',
            不良: 'table-level-717171'
          };
          const showText = handleTagNegativeShow(row.tagNegative, row.evaluationResult) || '';
          return <span class={'table-level ' + map[showText]}>{showText}</span>;
        }
      }
    },
    {
      title: '较上次变化',
      field: 'rateChangeTrend',
      width: 120,
      align: 'center',
      slots: {
        default: ({ row }) => {
          // 0不变，-1下降，1上升，2无可比较
          const map = {
            '1': { text: '上升', img: 'up.png' },
            '-1': { text: '下降', img: 'down.png' },
            '0': { text: '不变', img: 'unchanged.png' },
            '2': { text: '无可比较', img: 'unchanged.png' }
          };
          const trend = row.rateChangeTrend ? row.rateChangeTrend + '' : '0';
          return (
            <div class="trend-wrapper">
              <span>{map[trend].text}</span>
              <img v-show={row.rateChangeTrend !== 2} class="trend-suffix" src={getImageUrl(map[trend].img)} />
            </div>
          );
        }
      }
    },
    {
      title: '披露等级',
      field: 'evaluationResult',
      width: 100,
      align: 'center'
    },
    {
      title: '披露日期',
      field: 'noticeDate',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'noticeDate'
    },
    {
      title: '评价单位',
      field: 'postAgency',
      width: 200
    },
    { ...INFO_SOURCE('tagName') },
    { ...PROVINCE },
    { ...CITY },
    { ...COUNTRY },
    { ...INDUSTRY_CATEGORY },
    { ...BUSINESS_DATA },
    { ...REGISTERED_CAPITAL }
  ]);

  return {
    tableData,
    columns
  };
}
